﻿<%@ Control Language="C#" ClassName="CodeFormater" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Web" %>
<%@ Import Namespace="System.ComponentModel" %>
<%@ Import Namespace="Manoli.Utils.CSharpFormat" %>

<script runat="server">
    [PersistenceMode(PersistenceMode.InnerProperty)]
    public string Code { get; set; }

    [PersistenceMode(PersistenceMode.InnerProperty)]
    public ITemplate CodeTemplate { get; set; }
    public string SourceFile { get; set; }
    public bool ShowLines { get; set; }
    public string Language { get; set; }
    SourceFormat formatter;

    protected SourceFormat CreateFormat()
    {
        if (!string.IsNullOrEmpty(Language))
        {
            if (Language.ToLower() == "c#")
                return new CSharpFormat();
            if (Language.ToLower() == "javascript")
                return new JavaScriptFormat();
        }
        return new HtmlFormat();
    }

    protected override void CreateChildControls()
    {
        formatter = CreateFormat();
        formatter.LineNumbers = ShowLines;
        //formatter.EmbedStyleSheet = true;
        if (!string.IsNullOrEmpty(SourceFile))
        {
            string fileName = Context.Server.MapPath(SourceFile);
            Stream stream = File.OpenRead(fileName);
            lbCode.Text = formatter.FormatCode(stream);
            stream.Close();
            //writer.Write(formatter.FormatCode(stream));
        }

    }

    protected override void Render(HtmlTextWriter writer)
    {
        if (CodeTemplate != null)
        {
            PlaceHolder holder = new PlaceHolder();
            CodeTemplate.InstantiateIn(holder);
            Code = Context.Server.HtmlEncode(DNA.UI.ClientScriptManager.RenderControlToHTML(holder));
        }

        if (!string.IsNullOrEmpty(Code))
            writer.Write(formatter.FormatCode(Context.Server.HtmlDecode(Code)));
        else
            base.Render(writer);

    }
</script>

<div id="viewCode" style="float: none;">
    <a href='javascript:toggleCode();void(0);'><span class="ui-icon ui-icon-extlink"></span>
        View Code</a></div>
<br />
<br />
<div id="sampleCodeViewer" style="border: solid 1px #f3f3f3; display: none; overflow: scroll;">
    <asp:Label ID="lbCode" runat="server" Text=""></asp:Label>
</div>

<script type="text/javascript">
    var containerHeight = 0;
    function toggleCode() {
        $("#sampleCodeViewer").toggle();
        if (window.frameElement) {
            var bounds = Sys.UI.DomElement.getBounds(window.frameElement);
            var currentBounds = Sys.UI.DomElement.getBounds($get("sampleCodeViewer"));
            if ($("#sampleCodeViewer").css("display") == "block") {
                containerHeight = bounds.height;
                $(window.frameElement).css({ "height": (currentBounds.height + 550).toString() + "px" });
            }
            else {
                $(window.frameElement).css({ "height": containerHeight.toString() + "px" });
            }
        }
    }
</script>

